/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uns.fit.gis.xml;

import java.util.Stack;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import uns.fit.gis.Util;

/**
 *
 * @author nPLUS
 */
public class RegisterHandler extends DefaultHandler {
    private int id = Util.DEVICE_ID;
    private Stack tagStack;

    public RegisterHandler() {
        tagStack = new Stack();
    }

    public void startDocument() throws SAXException {
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //TODO: do something to process qName
        tagStack.push(qName);
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        String chars = new String(ch, start, length).trim();
        System.out.println(chars);
        if (chars.length() > 0) {
            String qName = (String) tagStack.pop();
            if (qName.equals("Id")) {
                id = Integer.valueOf(chars).intValue();
            } else if (qName.equals("Msg")) {
                if(chars.equals("OK"))
                    Util.DEVICE_ID = id;
            }
        }
    }

    public void endElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tagStack.pop();
    }

    public void endDocument() throws SAXException {
        System.out.println(Util.DEVICE_ID);
    }
}
